import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {


    public static void main(String[] args) {

        ThreadPoolExecutor  executor=new ThreadPoolExecutor(3, 5, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<>(3)
//                ,new ThreadPoolExecutor.AbortPolicy()// 抛异常
//                ,new ThreadPoolExecutor.CallerRunsPolicy()  //当前线程直接执行
//                ,new ThreadPoolExecutor.DiscardOldestPolicy()  //删除最早的线程，把当前线程加入到队列中
                ,new ThreadPoolExecutor.DiscardPolicy() //抛弃线程


        );


        for (int i = 0; i < 20; i++) {
            int finalI = i;
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getId() + "[" + finalI + "] -- 开始");

//                    try {
////                        Thread.sleep(5000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                    System.out.println(Thread.currentThread().getId() + "[" + finalI + "] -- 结束");
                }
            });
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        executor.shutdown();
        boolean flag = true;
        try {

            do {
                flag = ! executor.awaitTermination(1,TimeUnit.SECONDS);
            } while (flag);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("线程池关闭成功。。。");
        System.out.println(Thread.currentThread().getId());
    }
}
